package org.sqltomongo.configure;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.sqltomongo.SQLToMongoTemplate;
import org.sqltomongo.cache.data.ClearCacheListener;
import org.sqltomongo.cache.data.MongoTemplateProxy;
import org.sqltomongo.cache.data.SaveMongoEventListener;
import org.sqltomongo.orm.ConfigurationBuilder;
import org.sqltomongo.orm.DefaultSqlSession;
import org.sqltomongo.orm.SqlSession;
import org.sqltomongo.plugin.InterceptorConfigurer;
import org.sqltomongo.plugin.InterceptorConfigurerAdapter;

@Configuration
@EnableConfigurationProperties({ SqlToMongoProperties.class })
public class SqlToMongoAutoConfiguration {



    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = "sql-to-mongo.dataCache", name = "cacheEnabled", havingValue = "true")
    public MongoTemplateProxy mongoTemplate(@Autowired MongoDatabaseFactory mongoDbFactory,
                                            @Autowired org.sqltomongo.orm.Configuration configuration){
        return new MongoTemplateProxy(mongoDbFactory, configuration);
    }

    @Bean
    @ConditionalOnProperty(prefix = "sql-to-mongo.dataCache", name = "cacheEnabled", havingValue = "true")
    public SaveMongoEventListener saveMongoEventListener(@Autowired org.sqltomongo.orm.Configuration configuration){
        return new SaveMongoEventListener(configuration);
    }

    @Bean
    @ConditionalOnMissingBean
    public InterceptorConfigurer interceptorConfigurer(){
        return new InterceptorConfigurerAdapter();
    }

    @Bean
    @ConditionalOnMissingBean
    public org.sqltomongo.orm.Configuration configuration(@Autowired SqlToMongoProperties properties,
                                                          @Autowired InterceptorConfigurer interceptorConfigurer) throws Exception {
        return new ConfigurationBuilder().build(interceptorConfigurer, properties);
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = "sql-to-mongo.dataCache", name = "cacheEnabled", havingValue = "true")
    public ClearCacheListener clearCacheListener(@Autowired org.sqltomongo.orm.Configuration configuration){
        return new ClearCacheListener(configuration);
    }

    @Bean
    @ConditionalOnMissingBean
    @DependsOn({"mongoTemplate"})
    public SqlSession sqlSession(@Autowired(required = false) MongoTemplate mongoTemplate,
                                 @Autowired org.sqltomongo.orm.Configuration configuration) {
        return DefaultSqlSession.DefaultSqlSessionBuilder.build(mongoTemplate, configuration);
    }


    @Bean
    @ConditionalOnMissingBean
    public SQLToMongoTemplate sqlToMongoTemplate(@Autowired SqlSession sqlSession){
        return new SQLToMongoTemplate(sqlSession);
    }

}
